package com.xbongbong.paas.toolbox.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class CollectionsUtil {
	
	/**
	 * 两个整型列表取并集 
	 * @param list1
	 * @param list2
	 * @return
	 */
	public static List<Integer> union(List<Integer> list1,
                                      List<Integer> list2) {
		
		List<Integer> list = new ArrayList<Integer>();
		list.addAll(list1);
		list.addAll(list2);
		HashSet<Integer> hs = new HashSet<Integer>(list);
		list.clear();
		list.addAll(hs);
		return list;
	}
	
	/**
	 * map转为（key-Value）对象数组
	 * @param filterMap
	 * @return
	 * @author zheng.li
	 * @time 2017-4-10 上午11:16:18
	 */
	public static JSONArray mapToArray(Map<String,Object> filterMap){
		JSONArray filterArray = new JSONArray();
		if(filterMap == null){
			return filterArray;
		}
		for (Entry<String, Object> attrSet : filterMap.entrySet()) {
			String key = attrSet.getKey();
			Object value = attrSet.getValue();
			JSONObject obj = new JSONObject();
			obj.put("key", key);
			obj.put("value", value);
			filterArray.add(obj);
		}
		return filterArray;
		
	}

	/**
	 * 把empty的JSON数组转成null
	 *
	 * @param array 数组
	 * @author GuoJun.HU
	 * @since 3.19
	 * 创建时间 2018/3/6 16:44
	 */
    public static JSONArray emptyArray2Null(JSONArray array) {
        if (array != null && array.size() == 0) {
            array = null;
        }
        return array;
    }
    
	public static boolean isEmpty(Collection<?> collection) {
		return collection == null || collection.isEmpty();
	}
	
	public static boolean isNotEmpty(Collection<?> collection) {
		return collection != null && !collection.isEmpty();
	}
}
